<?php

namespace app\api\controller\supplier;

use addons\wechat\library\Config as ConfigService;
use AlibabaCloud\SDK\Iot\V20180120\Iot;
use AlibabaCloud\SDK\Iot\V20180120\Models\PubRequest;
use app\admin\model\AssistantOrder;
use app\admin\model\Chargecode;
use app\admin\model\Cmdlog;
use app\admin\model\Devicelock;
use app\admin\model\Linedevice;
use app\admin\model\litestore\Litestoreorder;
use app\admin\model\litestore\Litestoreorderaddress;
use app\admin\model\litestore\Litestoreordergoods;
use app\admin\model\Mallsupplier;
use app\admin\model\Org;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdaytrade;
use app\admin\model\Shopdevice;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shoproomset;
use app\admin\model\Shoptableorder;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userwithdraw;
use app\api\controller\Exception;
use App\Api\PrinterService;
use App\Api\PrintService;
use app\api\service\AirService;
use app\api\service\BalanceService;
use app\api\service\DeviceService;
use app\api\service\LockService;
use app\api\service\MpService;
use app\api\service\MtService;
use app\api\service\MtUtil;
use app\api\service\RoomService;
use app\api\service\VbillService;
use app\api\service\XhtMtService;
use app\common\controller\Api;
use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;
use Darabonba\OpenApi\Models\Config;
use EasyWeChat\Factory;
use fast\Http;
use PhpMqtt\Client\ConnectionSettings;
use PhpMqtt\Client\MQTTClient;
use think\Console;
use think\exception\HttpResponseException;
use think\Response;
use Workerman\Mqtt\Client;
use Workerman\Worker;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];



    public function index()
    {

        $user = $this->auth->getUser();
        $mallSupplierModel = new Mallsupplier();
        $supplier = $mallSupplierModel->where('mobile',$user['mobile'])->find();
        $litestoreOrderGoodModel = new Litestoreordergoods();
        $daiCount = $litestoreOrderGoodModel->where('supplier_id',$supplier['id'])->where('status','payed')->count('id');
        $daiAmount = $litestoreOrderGoodModel->where('supplier_id',$supplier['id'])->where('status','payed')->sum('goods_price');

        $totalCount = $litestoreOrderGoodModel->where('supplier_id',$supplier['id'])->where('status','<>','paying')->count('id');
        $totalAmount = $litestoreOrderGoodModel->where('supplier_id',$supplier['id'])->where('status','<>','paying')->sum('goods_price');


        $ret = array();
        $ret['daiCount'] = $daiCount;
        $ret['daiAmount'] = $daiAmount;
        $ret['totalCount'] = $totalCount;
        $ret['totalAmount'] = $totalAmount;

        $this->success('',$ret);
    }


    public function orderList()
    {

        $page = $this->request->param('page');
        $status = $this->request->param('status');
        $user = $this->auth->getUser();
        $mallSupplierModel = new Mallsupplier();
        $supplier = $mallSupplierModel->where('mobile',$user['mobile'])->find();
        $litestoreOrderGoodModel = new Litestoreordergoods();
        $listoreOrderModel = new Litestoreorder();
        $orderAddressModel = new Litestoreorderaddress();
        $ret = array();

        if($status != ""){
           $list =  $litestoreOrderGoodModel->group('order_id')->where('supplier_id',$supplier['id'])->where('status','payed')->select();
           $total  =  $litestoreOrderGoodModel->group('order_id')->where('supplier_id',$supplier['id'])->where('status','payed')->count('order_id');
           $totalAmount  =  $litestoreOrderGoodModel->where('supplier_id',$supplier['id'])->where('status','payed')->sum('goods_price');

           foreach ($list as $key=>$val){
              $order =  $listoreOrderModel->where('id',$val['order_id'])->find();
              $address =  $orderAddressModel->where('order_id',$val['order_id'])->find();
              $goods =  $litestoreOrderGoodModel->where('order_id',$val['order_id'])->where('supplier_id',$supplier['id'])->count('id');
              $list[$key]['order'] = $order;
               $address['detail'] = $address['Area']['province'].$address['Area']['city'].$address['Area']['region'].$address['detail'];
              $list[$key]['address'] = $address;
              $list[$key]['totalGoods'] = $goods;
              $list[$key]['createTimeTxt'] = date("Y-m-d H:i",$order['createtime']);
              $list[$key]['orderId'] = $order['id'];

           }
            $ret['list'] = $list;
            $ret['total'] = $total;
            $ret['totalAmount'] = $totalAmount;

            $this->success('',$ret);

        }else{
            $list =  $litestoreOrderGoodModel->group('order_id')->where('supplier_id',$supplier['id'])->where('status','<>','paying')->select();
            $total  =  $litestoreOrderGoodModel->group('order_id')->where('supplier_id',$supplier['id'])->where('status','<>','paying')->count('order_id');
            $totalAmount  =  $litestoreOrderGoodModel->where('supplier_id',$supplier['id'])->where('status','<>','paying')->sum('goods_price');

            foreach ($list as $key=>$val){
                $order =  $listoreOrderModel->where('id',$val['order_id'])->find();
                $address =  $orderAddressModel->where('order_id',$val['order_id'])->find();
                $goods =  $litestoreOrderGoodModel->where('supplier_id',$supplier['id'])->where('order_id',$val['order_id'])->count('id');
                $list[$key]['order'] = $order;
                $address['detail'] = $address['Area']['province'].$address['Area']['city'].$address['Area']['region'].$address['detail'];
                $list[$key]['address'] = $address;
                $list[$key]['totalGoods'] = $goods;
                $list[$key]['createTimeTxt'] = date("Y-m-d H:i",$order['createtime']);
                $list[$key]['orderId'] = $order['id'];

            }
            $ret['list'] = $list;
            $ret['total'] = $total;
            $ret['totalAmount'] = $totalAmount;

            $this->success('',$ret);
        }
    }

    public function orderGoodList()
    {

        $user = $this->auth->getUser();
        $mallSupplierModel = new Mallsupplier();
        $supplier = $mallSupplierModel->where('mobile',$user['mobile'])->find();
        $orderId = $this->request->param('order_id');
        $litestoreOrderGoodModel = new Litestoreordergoods();
        $list = $litestoreOrderGoodModel->where('supplier_id',$supplier['id'])->where('order_id',$orderId)->select();
        foreach ($list as $key=>$val){

            $list[$key]['images'] = cdnurl($val['images'],true);
        }
        $total =  $litestoreOrderGoodModel->where('order_id',$orderId)->count('id');
        $ret = array();
        $ret['list'] = $list;
        $ret['total'] = $total;
        $this->success('',$ret);
    }

    public function updateOrderKd()
    {
        $kd_num = $this->request->param('kd_num');
        $id = $this->request->param('id');
        $ids = json_decode(htmlspecialchars_decode($id), true);
        $listoreOrderModel = new Litestoreorder();

        $litestoreOrderGoodModel = new Litestoreordergoods();

        $orderId = -1;
        foreach ($ids as $key=>$val){
            $litestoreOrderGoodModel->where('id',$val)->update(['kd_num'=>$kd_num,'status'=>'sending']);
            $good = $litestoreOrderGoodModel->where('id',$val)->find();
            $orderId = $good['order_id'];
        }

        $listoreOrderModel->where('id',$orderId)->update(['freight_status'=>20]);
        $this->success('成功');
    }



    public function detail()
    {


        $user = $this->auth->getUser();
        $mallSupplierModel = new Mallsupplier();
        $supplier = $mallSupplierModel->where('mobile',$user['mobile'])->find();

        $supplierModel = new Mallsupplier();
        $supplier = $supplierModel->where('id',$supplier['id'])->find();
        $supplier['yyzz'] = cdnurl($supplier['yyzz'],true);
        $supplier['zz'] = cdnurl($supplier['zz'],true);
        $supplier['ya_img'] = cdnurl($supplier['ya_img'],true);

        $this->success('',$supplier);
    }


}

